package com.xjh.basestudy.javabase.Collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestList {
    public static void main(String[] args) {
        //创建集合对象
        List<String> list = new ArrayList<String>();
        //添加元素
       list.add("hello");
       list.add("world");
       list.add("java");
       list.add("java");//可重复
       System.out.println(list);
       //迭代器的方式遍历
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);

        }
        //在指定位置插入元素
        list.add(1,"hahah");
        //删除
        list.remove(2);
        //修改指定位置的值
        list.set(1,"sds");
        //返回指定索引位置的值
        System.out.println(list.get(2));
        //通过get方法遍历集合
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));
        System.out.println(list.get(3));
        //for 循环改进遍历
        for(int i=0;i<list.size();i++){
            String s =list.get(i);
            System.out.println(s);
        }

    }
}
